package org.yylnb.openplatform.websocket.listener;

import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.yylnb.openplatform.commons.bean.PayMQBean;
import org.yylnb.openplatform.websocket.handler.MyTextWebSocketHandler;

import java.io.IOException;
import java.util.Map;

/**
 * @author RainLin
 * @date 2020/7/28 - 21:02
 */
@Component
public class SubPaymentListener {

    @StreamListener("paymentresult")
    public void onMessage(PayMQBean payMQBean) {
        System.err.println("socket收到支付的结果了");
        Map<String, WebSocketSession> allClients = MyTextWebSocketHandler.getAllClients();

        WebSocketSession session = allClients.get(payMQBean.getOid());
        if (session != null&&session.isOpen()) {
            try {
                session.sendMessage(new TextMessage("1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

